/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.CC;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.embeddable.EJBContainer;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.naming.Context;
import javax.naming.NamingException;
import merlion.common.entity.Staff;
import merlion.crm.slcm.entity.Customer;
import merlion.frm.ccm.session.CCSessionBean;
import merlion.frm.ccm.session.CCSessionBeanLocal;
import merlion.frm.ccm.entity.CreditGroup;
import merlion.frm.ccm.entity.CreditAccount;
import merlion.crm.slcm.session.SLCSessionBean;
import merlion.crm.slcm.session.SLCSessionBeanLocal;

/**
 *
 * @author Zhiqi
 */
@ManagedBean(name = "caManagerBean")
@ViewScoped
public class CreditAccountManagerBean {

    @EJB
    private CCSessionBeanLocal ccSessionBean;
    private SLCSessionBeanLocal sLCSessionBean;
    private CreditAccount ca;
    private String statusMsg;
    private Collection<CreditGroup> cgs;
    private Collection<Customer> customers;

    //--------------------------------------------------------------------------
    //                              Constructor
    //--------------------------------------------------------------------------
    public CreditAccountManagerBean() {
        ca = new CreditAccount();
    }

    @PostConstruct
    public void init() {
        CreditAccount creditAccount = (CreditAccount) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("CA");
        statusMsg = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("statusMsg");
        if (creditAccount != null) {
            ca = creditAccount;
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("CA");
        } else {
            ca = new CreditAccount();
            ca.setCustomer(new Customer());
        }
    }

    //--------------------------------------------------------------------------
    //                   Credit Account Manager Bean Methods
    //--------------------------------------------------------------------------
    public Collection<CreditGroup> getAllCgs() {
        setCgs(ccSessionBean.getAllCreditGroup());
        return cgs;
    }

    public boolean renderDSCreditAmount() {
        if (ca.getCustomer().getCategory().equals("DirectSale")) {
            return true;
        }

        return false;
    }

    public boolean renderWSCreditAmount() {
        if (ca.getCustomer().getCategory().equals("WholeSale")) {
            return true;
        }

        return false;
    }

    public void setCreditGroup() {
        CreditGroup cg = ca.getCreditGroup();
        ca.setCreditGroup(ccSessionBean.getCreditGroupById(cg.getId()));
    }

    public String createCreditAccount() {

        //Validate
        boolean valid = true;
        //Check credit group
        if (ca.getCreditGroup().getId() == -1) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Credit Group cannot be empty", ""));
            valid = false;
        }

        //If valid persist
        if (valid) {

            ca = ccSessionBean.createCreditAccount(ca);

            if (ca != null) {
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("statusMsg", "Credit account created successfully.");
            } else {
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("statusMsg", "Transaction to create credit account has failed. Please try again.");
            }
            return "viewCreditAccountStatus?faces-redirect=true";
        }

        return null;
    }

    public String viewCreditAccount() {
        ca = ccSessionBean.getCustomerCreditAccount(ca.getCustomer());

        if (ca != null) {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("CA", ca);
            return "viewCreditAccountDisplay?faces-redirect=true";
        }
        else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Customer does not have a credit account", ""));
            return null;
        }
    }

    public String updateCreditAccount() {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("CA", ca);
        return "viewCreditAccountEdit?faces-redirect=true";
    }

    public String closeCreditAccount() {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("statusMsg", "View Credit Account is closed.");
        return "viewCreditAccountStatus?faces-redirect=true";
    }
    
    public Collection<Customer> getAllCustomerNoCreditAccount(){
        //customers without credit account
        return ccSessionBean.getCustomersNoCreditAccount();
    }
    
    public Collection<Customer> getAllCustomerWithCreditAccount(){
        //customer with credit account
        return ccSessionBean.getCustomersWithCreditAccount();
    }

    //--------------------------------------------------------------------------
    //                           Getter and Setter
    //--------------------------------------------------------------------------
    public CreditAccount getCa() {
        return ca;
    }

    public void setCa(CreditAccount ca) {
        this.ca = ca;
    }

    public Collection<CreditGroup> getCgs() {
        return cgs;
    }

    public void setCgs(Collection<CreditGroup> cgs) {
        this.cgs = cgs;
    }

    public String getStatusMsg() {
        return statusMsg;
    }

    public void setStatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }

    public Collection<Customer> getCustomers() {
        return customers;
    }

    public void setCustomers(Collection<Customer> customers) {
        this.customers = customers;
    }
    
}
